// pages/posts/posts-detail.js
import { newsDetail } from '../../../data/post-data'

let app = getApp()
const globalData = app.globalData

Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const postId = options.id
    const postDetail = newsDetail[postId]
    this.setData({
      postDetail,
      currId: postId
    })
    //获取所有文章的收藏情况
    let postsCollected = wx.getStorageSync('posts_collected')
    //如果已经有缓存
    if (postsCollected) {
      //获取当前文章的收藏情况
      let postCollected = postsCollected[postId]
      if (postCollected) {
        this.setData({
          collected: postCollected
        })
      }
    } else {//没有缓存的情况
      let postsCollected = {}
      postsCollected[postId] = false
      //设置收藏状态缓存
      wx.setStorageSync('posts_collected', postsCollected)
    }
    if (globalData.g_isMusicPlay && globalData.g_currMusicId === postId) {
      this.setData({
        isMusicPlay: true
      })
    }
    this.setMusicMonitor();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log(this.data)
  },

  onTapCollection: function (event) {
    let postsCollected = wx.getStorageSync('posts_collected')
    let postCollected = postsCollected[this.data.currId]
    postCollected = !postCollected
    postsCollected[this.data.currId] = postCollected
    this.showToast(postsCollected, postCollected)
    //this.showModal(postsCollected, postCollected)
  },

  onTapShare: function () {
    let itemList = ['分享到微信', '分享到朋友圈', '分享到QQ', '分享到微博']
    wx.showActionSheet({
      itemList: itemList,
      success: function (res) {
        const index = res.tapIndex
        wx.showModal({
          title: `成功${itemList[index]}`,
          content: '是否前往',
          cancelText: '取消',
          confirmText: '前往',
        })
      }
    })
  },

  onTapMusic: function (event) {
    let currId = this.data.currId
    let isMusicPlay = this.data.isMusicPlay
    if (isMusicPlay) {
      wx.pauseBackgroundAudio()
      this.setData({
        isMusicPlay: false
      })
    } else {
      wx.playBackgroundAudio({
        dataUrl: newsDetail[currId].music.url,
        title: newsDetail[currId].music.title,
        coverImgUrl: newsDetail[currId].music.coverImg
        // callback functions, success & fail & complete
      })
      this.setData({
        isMusicPlay: true
      })
    }
    globalData.g_isMusicPlay = this.data.isMusicPlay
  },

  showToast: function (postsCollected, postCollected) {
    let _this = this
    wx.setStorageSync('posts_collected', postsCollected)
    _this.setData({
      collected: postCollected
    })
    wx.showToast({
      title: postCollected ? '收藏成功' : '取消收藏',
      icon: 'success',
      duration: 1000
    })
  },

  showModal: function (postsCollected, postCollected) {
    let _this = this
    wx.showModal({
      title: '收藏文章',
      content: postCollected ? '选择收藏该文章？' : '确定取消收藏文章？',
      showCancel: 'true',
      cancelText: '取消',
      confirmText: '确认',
      confirmColor: '#3cc51f',
      success: function (res) {
        if (res.confirm) {
          wx.setStorageSync('posts_collected', postsCollected)
          _this.setData({
            collected: postCollected
          })
        }
      }
    })
  },

  setMusicMonitor: function () {
    let _this = this
    wx.onBackgroundAudioPause(function () {
      _this.setData({
        isMusicPlay: false
      })
      globalData.g_currMusicId = null
    })
    wx.onBackgroundAudioPlay(function () {
      _this.setData({
        isMusicPlay: true
      })
      globalData.g_currMusicId = _this.data.currId
    })
    wx.onBackgroundAudioStop(function () {
      _this.setData({
        isMusicPlay: false
      })
      globalData.g_currMusicId = null
    })
    globalData.g_isMusicPlay = _this.data.isMusicPlay
  }
})